package com.jz.kufangkuqu.kuwei.controller;


import com.jz.kufangkuqu.kuwei.bean.KuWeiAdd;
import com.jz.kufangkuqu.kuwei.bean.KuWeiSelect;
import com.jz.kufangkuqu.kuwei.bean.KuWeiUpdate;
import com.jz.kufangkuqu.kuwei.bean.KuWeiUpdatestatus;
import com.jz.kufangkuqu.kuwei.service.KuWeiService;
import com.jz.util.ResultBuildVO;
import com.jz.util.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;


@RestController
@CrossOrigin
public class KuWeiController {

    @Autowired
    private KuWeiService kuWeiService;


    //分页
    @PostMapping("StorageBin/queryPage")
    public ResultVO querypage(@RequestBody KuWeiSelect kuWeiSelectVo){
        if(kuWeiSelectVo.getPageIndex()==null){
            kuWeiSelectVo.setPageIndex(1);
        }
        if(kuWeiSelectVo.getPageSize()==null){
            kuWeiSelectVo.setPageSize(10);
        }
        ResultVO resultVO = kuWeiService.queryPage(kuWeiSelectVo);
        return resultVO;
    }

    //新增
    @PostMapping("StorageBin/add")
    public ResultVO add(@RequestBody @Valid KuWeiAdd kuWeiAdd, BindingResult bindingResult ){
        if (bindingResult.hasErrors()){
            return ResultBuildVO.error(bindingResult.getFieldError().getDefaultMessage());
        }
        ResultVO resultVO = kuWeiService.add(kuWeiAdd);
        return resultVO;
    }

    //修改
    @PostMapping("StorageBin/update")
    public ResultVO update(@RequestBody @Valid KuWeiUpdate kuWeiUpdate, BindingResult bindingResult ){
        if (bindingResult.hasErrors()){
            return ResultBuildVO.error(bindingResult.getFieldError().getDefaultMessage());
        }
        ResultVO resultVO = kuWeiService.update(kuWeiUpdate);
        return resultVO;
    }


    //启用禁用
    @PostMapping("StorageBin/updateStatus")
    public ResultVO updateStatus(@RequestBody @Valid KuWeiUpdatestatus kuWeiUpdatestatus, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultBuildVO.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return kuWeiService.updateStatus(kuWeiUpdatestatus);
    }

}